package net.sync.disruptor.demo1;

import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestDisruptor {
    static ExecutorService executor = Executors.newFixedThreadPool(4);

    public static void main(String[] args) {
        try {
            Disruptor<MyEvent> disruptor = new Disruptor(new MyEventFactory(),
                    1024, executor,
                    ProducerType.SINGLE, new YieldingWaitStrategy());

            disruptor.handleExceptionsWith(new MyHandlerException());
            disruptor.handleEventsWithWorkerPool(new Handler1())
                    .thenHandleEventsWithWorkerPool(new Handler2())
                    .thenHandleEventsWithWorkerPool(new Handler3());

            disruptor.start();

            MyEventProduce ep = new MyEventProduce().setDisruptor(disruptor);
            CountDownLatch countDownLatch = ep.getCountDownLatch();
            executor.submit(ep);
            countDownLatch.await();
            disruptor.shutdown();

            System.out.println("运行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
